<?php
// 定义密钥
define('SECRET_KEY', 'ajedJWWI=xsJ3423');

// 获取请求的密钥
$apiKey = isset($_GET['key']) ? $_GET['key'] : '';

// 验证密钥
if ($apiKey !== SECRET_KEY) {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => '无效的密钥']);
    exit;
}

$userDir = 'user';
$messagesDir = 'messages';
$chatDir = 'ql';

// 获取所有用户
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_users') {
    $users = array_filter(glob("$userDir/*"), 'is_dir');
    $userNames = array_map('basename', $users);
    echo json_encode(['success' => true, 'users' => $userNames]);
    exit;
}

// 删除用户
if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && isset($_GET['action']) && $_GET['action'] === 'delete_user') {
    $userName = isset($_GET['username']) ? $_GET['username'] : '';
    $userPath = "$userDir/$userName";

    if (is_dir($userPath)) {
        array_map('unlink', glob("$userPath/*.*")); // 删除文件
        rmdir($userPath); // 删除文件夹
        echo json_encode(['success' => true, 'message' => "用户 $userName 删除成功"]);
    } else {
        echo json_encode(['success' => false, 'message' => "用户 $userName 不存在"]);
    }
    exit;
}

// 处理公共聊天
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'send_public_message') {
    $content = isset($_POST['content']) ? $_POST['content'] : '';
    $user = isset($_POST['user']) ? $_POST['user'] : '';
    $timestamp = date('Y-m-d H:i:s');

    if (empty($content) || empty($user)) {
        echo json_encode(['success' => false, 'message' => '内容和用户名不能为空']);
        exit;
    }

    $message = json_encode(['user' => $user, 'content' => $content, 'timestamp' => $timestamp]);
    file_put_contents("$messagesDir/public_chat.json", $message . "\n", FILE_APPEND);
    echo json_encode(['success' => true, 'message' => '消息发送成功']);
    exit;
}

// 查询公共消息
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_public_messages') {
    if (!file_exists("$messagesDir/public_chat.json")) {
        echo json_encode(['success' => false, 'message' => '没有找到公共消息记录']);
        exit;
    }
    
    $messages = file($messagesDir . '/public_chat.json', FILE_IGNORE_NEW_LINES);
    $messages = array_map('json_decode', $messages);
    
    echo json_encode(['success' => true, 'messages' => $messages]);
    exit;
}

// 删除特定消息
if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && isset($_GET['action']) && $_GET['action'] === 'delete_message') {
    // 逻辑来删除特定消息
}

// 私聊功能
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_shared_chats') {
    // 获取私聊文件列表
    $sharedChats = glob("$messagesDir/shared_chats/*.json");
    $chatList = array_map('basename', $sharedChats);
    echo json_encode(['success' => true, 'shared_chats' => $chatList]);
    exit;
}

// 删除私聊记录
if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && isset($_GET['action']) && $_GET['action'] === 'delete_shared_chat') {
    $chatFileName = isset($_GET['chat_file']) ? $_GET['chat_file'] : '';
    if (file_exists("$messagesDir/shared_chats/$chatFileName")) {
        unlink("$messagesDir/shared_chats/$chatFileName");
        echo json_encode(['success' => true, 'message' => '私聊记录删除成功']);
    } else {
        echo json_encode(['success' => false, 'message' => '私聊记录不存在']);
    }
    exit;
}

// 群聊功能
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'get_group_chats') {
    // 获取群聊信息
    $groupInfo = json_decode(file_get_contents("$chatDir/info.json"), true);
    echo json_encode(['success' => true, 'group_info' => $groupInfo]);
    exit;
}

// 指定用户名发送群聊消息
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'send_group_message') {
    $group = isset($_POST['group']) ? $_POST['group'] : '';
    $user = isset($_POST['user']) ? $_POST['user'] : '';
    $content = isset($_POST['content']) ? $_POST['content'] : '';
    $timestamp = date('Y-m-d H:i:s');

    if (empty($group) || empty($user) || empty($content)) {
        echo json_encode(['success' => false, 'message' => '群聊名称、用户名和内容不能为空']);
        exit;
    }

    $message = json_encode(['sender' => $user, 'content' => $content, 'timestamp' => $timestamp]);
    $groupPath = "$chatDir/$group.json";

    // 如果文件不存在，创建新文件
    if (!file_exists($groupPath)) {
        file_put_contents($groupPath, '');
    }
    
    file_put_contents($groupPath, $message . "\n", FILE_APPEND);
    echo json_encode(['success' => true, 'message' => '群聊消息发送成功']);
    exit;
}
?>